﻿using System;
using System.Collections.Generic;
using System.Text;
using lin;

public class LoginHandler
{
    public LoginHandler(Dictionary<Protocol, Handler> handlers)
    {
        handlers.Add(Protocol.ReqLogin,OnLogin);//登录消息
        handlers.Add(Protocol.ReqMatch, OnMatch);//注册消息
    }

    private void OnLogin(UserToken token, byte[] bytes)
    {
        User user = Util.Deserialize<User>(bytes);

        //重复登录
        if (UserManager.Instance.HasUser(user.username))
        {
            RspLogin rsp = new RspLogin();
            rsp.username = user.username;
            rsp.code = 1;//重复登录

            Server.Send(token, Protocol.ReqLogin, rsp);
        }
        else
        {
            RspLogin rsp = new RspLogin();
            rsp.username = user.username;
            rsp.code = 0;//登录成功

            token.user = user;
            UserManager.Instance.AddUser(token);
            Server.Send(token, Protocol.ReqLogin, rsp);
        }
    }

    private void OnMatch(UserToken token, byte[] bytes)
    {
        ReqMatch req = Util.Deserialize<ReqMatch>(bytes);

        //请求经典模式
        if (req.mode == 1)
        {
            Room rm = RoomManager.Instance.GetOneRoom();
            rm.AddUser(token);
        }
    }
}